<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar left-arrow @click-left="tomy" title="编辑资料"></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userinfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        @click="changeusername"
        is-link
        title="名称"
        :value="userinfo.name"
      >
      </van-cell>
      <van-cell
        is-link
        title="性别"
        @click="showsex = !showsex"
        :value="userinfo.gender == 0 ? '男' : '女'"
      />
      <van-cell
        @click="showbirthdaypop"
        is-link
        title="生日"
        :value="userinfo.birthday"
      />
    </van-cell-group>
    <!-- 编辑名字弹框 -->
    <van-dialog
      @confirm="savename"
      v-model="showeditName"
      title="修改昵称"
      show-cancel-button
    >
      <slot
        ><van-field
          v-model="newuserName"
          placeholder="请输入新的昵称"
          ref="nameField"
      /></slot>
    </van-dialog>
    <!-- 性别弹框 -->
    <van-popup v-model="showsex" position="bottom" :style="{ height: '20%' }">
      <slot>
        <van-nav-bar
          title="修改性别"
          left-text="取消"
          @click-left="showsex = !showsex"
        />
        <van-cell-group>
          <van-cell title="男" is-link @click="changegender(0)" />
          <van-cell title="女" is-link @click="changegender(1)" />
        </van-cell-group>
      </slot>
    </van-popup>
    <!-- 生日弹框 -->
    <van-popup v-model="showbirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="confirmbirthday"
        @cancel="cancelbirthday"
      />
    </van-popup>
    <!-- 裁切区域 -->
    <div class="cropper-box" v-show="showcropper">
      <vueCropper
        ref="cropper"
        :img="userimg"
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
        fixed
      ></vueCropper>
      <van-button class="confirm-btn" type="primary" @click="submitphoto"
        >确认</van-button
      >
      <van-button class="cancel-btn" type="info" @click="showcropper = false"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { edituserInfo, editusericon } from '../../api/edit'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  components: {
    VueCropper
  },
  data () {
    return {
      showeditName: false,
      showsex: false,
      showbirthday: false,
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(2021, 6, 25),
      currentDate: new Date(1996, 5, 11),
      newuserName: '',
      showcropper: false,
      userimg: ''
    }
  },
  methods: {
    tomy () {
      this.$router.push({ path: '/my' })
    },
    // 裁切功能
    submitphoto () {
      this.$refs.cropper.getCropBlob(blob => {
        console.log(blob)
        const data = new FormData()
        data.append('photo', blob)
        // 发送请求
        editusericon(data).then(res => {
          console.log(res)
          this.$toast('更新成功')
          this.$store.commit('changeinfo', {
            ...this.userinfo,
            photo: res.data.data.photo
          })
          this.showcropper = false
        })
      })
    },
    // 头像上传
    afterRead (file) {
      this.showcropper = true
      // 此时可以自行将文件上传至服务器
      console.log(file)
      this.userimg = file.content
      // const data = new FormData()
      // data.append('photo', file.file)
      // // 发送请求
      // editusericon(data).then(res => {
      //   console.log(res)
      //   this.$toast('修改成功')
      //   this.$store.commit('changeinfo', {
      //     ...this.userinfo,
      //     photo: res.data.data.photo
      //   })
      // })
    },
    // 点击获取焦点
    changeusername () {
      this.showeditName = true
      this.newuserName = this.userinfo.name
      this.$nextTick(() => {
        this.$refs.nameField.focus()
      })
    },
    // 改变名字
    savename () {
      edituserInfo({ name: this.newuserName }).then(res => {
        this.$store.commit('changeinfo', {
          ...this.userinfo,
          name: res.data.data.name
        })
      })
      this.$toast('修改成功')
    },
    // 改变性别
    changegender (gender) {
      edituserInfo({ gender }).then(res => {
        this.$store.commit('changeinfo', {
          ...this.userinfo,
          gender
        })
      })
      this.$toast('修改成功')
      this.showsex = false
    },
    // 改生日
    showbirthdaypop () {
      this.showbirthday = true
      this.currentDate = new Date(this.userinfo.birthday)
    },
    confirmbirthday () {
      this.showbirthday = false
      const birthday = moment(this.currentDate).format('YYYY-MM-DD')
      this.$toast('修改成功')
      edituserInfo({ birthday }).then(() => {
        this.$store.commit('changeinfo', {
          ...this.userinfo,
          birthday
        })
      })
    },
    cancelbirthday () {
      this.showbirthday = false
    }
  },
  computed: mapState(['userinfo'])
  // computed: {
  //   userinfo () {
  //     return this.$store.state.userinfo
  //   }
  // }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    position: relative;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }
  .van-dialog {
    padding: 0 10px;
  }
  .van-dialog__content {
    border: 2px solid #e6e6e6;
  }
  .birthday-bar {
    background-color: #fff;
    overflow: hidden;
    .van-nav-bar__title {
      color: black;
    }
    .van-nav-bar__text {
      color: #fff;
    }
  }
  .van-uploader {
    position: absolute;
    left: 50%;
    transform: translate(-50%);
    width: 120px;
    height: 120px;
  }
  .van-uploader__upload {
    width: 120px;
    height: 120px;
    opacity: 0;
  }
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
